package com.hgy.crowd.handler;


import com.hgy.crowd.constant.CrowdConstant;
import com.hgy.crowd.service.MemberService;
import com.hgy.crowd.util.ResultEntity;
import com.hgy.crowd.vo.MemberLoginVO;
import com.hgy.crowd.vo.MemberVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


/**
 * @Author He
 * @Date 2020/7/1 10:28
 */
@Controller
public class MemberHandler {
    @Resource
    private MemberService memberService;
    @ResponseBody
    @RequestMapping("/auth/member/send/short/message.json")
    public ResultEntity sendMessageService(@RequestParam("phoneNum") String phoneNum){
        ResultEntity resultEntity=memberService.sendMessageService(phoneNum);
        return resultEntity;
    }
    @RequestMapping("/auth/do/member/register")
    public String registryMember(MemberVO memberVO, Model model){
        ResultEntity resultEntity = memberService.registryMember(memberVO);
        if (resultEntity.getResult().equals(ResultEntity.SUCCESS)){
            model.addAttribute("message","注册成功");
            return "redirect:http://localhost/auth/member/to/login/page";
        }else {
            model.addAttribute("message",resultEntity.getMessage());
            return "member-reg";
        }
    }
    @RequestMapping("/auth/member/do/login")
    public String login(@RequestParam("loginacct") String loginacct,
                        @RequestParam("userpswd") String userpswd,
                        ModelMap modelMap,
                        HttpSession session){
        ResultEntity result = memberService.login(loginacct, userpswd);
        if (ResultEntity.SUCCESS.equals(result.getResult())){
            MemberLoginVO memberLoginVO=(MemberLoginVO) result.getData();
            session.setAttribute(CrowdConstant.MEMBER_LOGGING_KEY,memberLoginVO);
            return "redirect:http://localhost/auth/member/to/center/page";
        }else {
            modelMap.addAttribute("message",result.getMessage());
            return "redirect:http://localhost/auth/member/to/login/page";
        }
    }
    @RequestMapping("/auth/member/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:http://localhost";
    }

}
